package com.yzg.controller;

import com.yzg.exceptionhandler.ServiceException;
import com.yzg.pojo.Product;
import com.yzg.pojo.vo.DiscountVO;
import com.yzg.pojo.vo.ProductSearch;
import com.yzg.service.ProductService;
import com.yzg.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: decade
 * @Description: 商品管理
 * @Date: 2021/1/13 20:31
 **/
@Api(tags = "ProductController", description = "商品管理")
@RestController
@RequestMapping("/pms/product")
@CrossOrigin
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/getList")
    @ApiOperation(value = "获取商品列表")
    @ApiImplicitParam(name = "productSearch",value = "商品查询条件",required = false,paramType = "ProductSearch")
    public R getList( ProductSearch productSearch){
        Map<String,Object> map = productService.getProductList(productSearch.getPageNum(),productSearch.getPageSize(),productSearch);
        return R.ok().data(map);
    }

    @PutMapping("/update/publishStatus")
    @ApiOperation(value = "商品上下架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "Long"),
            @ApiImplicitParam(name = "publishStatus",value = "上下架状态",required = true,paramType = "Integer")
    })
    public R publishStatus(Long id,Integer publishStatus){
        Product product = new Product();
        product.setId(id);
        if(publishStatus==null){
            throw new ServiceException(201,"参数不能为空");
        }
        if(publishStatus==0||publishStatus==1){
            product.setPublishStatus(publishStatus);
        }else{
            throw new ServiceException(201,"参数不合法");
        }
        Boolean row = productService.updateById(product);
        if(row){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PutMapping("/update/newStatus")
    @ApiOperation(value = "修改商品是否新品状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "Long"),
            @ApiImplicitParam(name = "newStatus",value = "是否新品状态",required = true,paramType = "Integer")
    })
    public R newStatus(Long id,Integer newStatus){
        Product product = new Product();
        product.setId(id);
        if(newStatus==null){
            throw new ServiceException(201,"参数不能为空");
        }
        if(newStatus==0||newStatus==1){
            product.setNewStatus(newStatus);
        }else{
            throw new ServiceException(201,"参数不合法");
        }
        Boolean row = productService.updateById(product);
        if(row){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PutMapping("/update/recommandStatus")
    @ApiOperation(value = "修改商品是否推荐状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "Long"),
            @ApiImplicitParam(name = "recommandStatus",value = "是否推荐状态",required = true,paramType = "Integer")
    })
    public R recommandStatus(Long id,Integer recommandStatus){
        Product product = new Product();
        product.setId(id);
        if(recommandStatus==null){
            throw new ServiceException(201,"参数不能为空");
        }
        if(recommandStatus==0||recommandStatus==1){
            product.setRecommandStatus(recommandStatus);
        }else{
            throw new ServiceException(201,"参数不合法");
        }
        Boolean row = productService.updateById(product);
        if(row){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取商品信息")
    @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "Long")
    public R getProductById(@PathVariable Long id){
        return R.ok().data("product",productService.getById(id));
    }

    @PutMapping("/update")
    @ApiOperation(value = "商品编辑")
    @ApiImplicitParam(name = "product",value = "商品信息",required = true,paramType = "Product")
    public R updateProduct(Product product){
        Boolean row = productService.updateById(product);
        if(row){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "商品添加")
    @ApiImplicitParam(name = "product",value = "商品信息",required = true,paramType = "Product")
    public R addProduct(Product product){
        Boolean row = productService.save(product);
        if(row){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping("/delete/{id}")
    @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "Long")
    @ApiOperation(value = "商品删除")
    public R delete(@PathVariable Long id){
        int row = productService.deleteProductById(id);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("/getDiscount/{id}")
    @ApiOperation(value = "根据商品id获取优惠信息")
    @ApiImplicitParam(name = "id",value = "商品id",required = true,paramType = "Long")
    public R getDiscount(@PathVariable Long id){
        DiscountVO discountVO = productService.getDiscount(id);
        return R.ok().data("discountVO",discountVO);
    }

    @PutMapping("/updateDiscount")
    @ApiOperation(value = "根据商品id修改优惠信息")
    @ApiImplicitParam(name = "discountVO",value = "优惠信息",required = true,paramType = "DiscountVO")
    public R updateDiscount(DiscountVO discountVO){
        int row = productService.updateDiscount(discountVO);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

}
